package com.longqinuo.mycarservice.manager;

import android.content.Context;
import android.database.Cursor;

import com.longqinuo.mycarservice.dao.DaoImpl;
import com.longqinuo.mycarservice.dao.IDao;
import com.longqinuo.mycarservice.dao.IServerDao;
import com.longqinuo.mycarservice.dao.ServerDaoImpl;
import com.longqinuo.mycarservice.entity.KeepServer;
import com.longqinuo.mycarservice.entity.SecondCar;


/**
 * 自定义代理类 去访问dao层 数据操作类
 * Created by zxw on 2016/5/20.
 */
public class MangerImpl implements IManager {
    private IDao dao;
    private IServerDao serverDao;
    public MangerImpl(Context context,Object o) {
        if (o instanceof SecondCar) {
            this.dao = new DaoImpl(context);
        } else if (o instanceof KeepServer) {
            this.serverDao = new ServerDaoImpl(context);
        }
    }
    @Override
    public long add(Object o) {
        long result = 0;
        if (dao != null) {
            result = dao.add((SecondCar) o);
        }
        if (serverDao != null) {
            result = serverDao.add((KeepServer) o);
        }

        return result;
    }

    @Override
    public Cursor query(Object o) {
        Cursor cursor = null;
        if (dao != null) {
            cursor = dao.findCar((SecondCar) o);
        }
        if (serverDao != null) {
            cursor = serverDao.findServer((KeepServer) o);
        }
        return cursor;
    }

    @Override
    public Cursor queryAll() {
        Cursor cursor = null;
        if (dao != null) {
            cursor = dao.findAllCars();
        }
        if (serverDao != null) {
            cursor = serverDao.findAllServers();
        }
        return cursor;
    }

    @Override
    public int delete(Object o) {
        int i = 0;
        if (dao != null) {
            i = dao.delete((SecondCar) o);
        }
        if (serverDao != null) {
            i = serverDao.delete((KeepServer) o);
        }
        return i;
    }

    @Override
    public boolean isExists(int id) {
        boolean b = false;
        if (dao != null) {
            b = dao.isExists(id);
        }
        if (serverDao != null) {
            b = serverDao.isExists(id);
        }
        return b;
    }
}
